﻿package com.kuanter.kuanterauto.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.view.CustomExpandableListView;
import com.kuanter.kuanterauto.view.CustomExpandableListView.QQHeaderAdapter;

/**
 * 选择车型适配器
 * @author soup
 *
 */
public class TestExpandablelistAdapter extends BaseExpandableListAdapter implements QQHeaderAdapter {

	private CustomExpandableListView expandable;
	private Context mContext;
	private ArrayList<String> groupList;
	private ArrayList<ArrayList<String>> childList;
	private ArrayList<ArrayList<Integer>> childList_IMG;

	public TestExpandablelistAdapter(Context context, CustomExpandableListView expandable, ArrayList<String> groupList,
			ArrayList<ArrayList<String>> childList,ArrayList<ArrayList<Integer>> childList_IMG) {
		this.mContext = context;
		this.expandable = expandable;
		this.groupList = groupList;
		this.childList = childList;
		this.childList_IMG=childList_IMG;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groupList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return childList.get(groupPosition).size();
	}

	/**
	 * 返回当前组的内容信息
	 */
	@Override
	public Object getGroup(int groupPosition) {
		String content = groupList.get(groupPosition);
		return content;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.expandablelist_group_item, null);
			viewHolder = new ViewHolder();

			viewHolder.textView = (TextView) convertView.findViewById(R.id.group_tv);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.textView.setText(groupList.get(groupPosition));
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.expandablelist_child_item_wy, null);
			viewHolder = new ViewHolder();

			viewHolder.textView = (TextView) convertView.findViewById(R.id.tv_pp);
			viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv_pp);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.textView.setText(childList.get(groupPosition).get(childPosition));
		viewHolder.iv.setImageResource(childList_IMG.get(groupPosition).get(childPosition));
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	class ViewHolder {
		TextView textView;
		ImageView iv;
	}

	/**
	 * 一下是实现借口的方法
	 */
	@Override
	public int getQQHeaderState(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		final int childCount = getChildrenCount(groupPosition);
		if (childPosition == childCount - 1) {
			return PINNED_HEADER_PUSHED_UP;
		} else if (childPosition == -1 && !expandable.isGroupExpanded(groupPosition)) {
			return PINNED_HEADER_GONE;
		} else {
			return PINNED_HEADER_VISIBLE;
		}
	}

	@Override
	public void configureQQHeader(View header, int groupPosition, int childPosition, int alpha) {
		((TextView) header.findViewById(R.id.group_tv)).setText(groupList.get(groupPosition));
	}

	private HashMap<Integer, Integer> groupStatusMap = new HashMap<Integer, Integer>();

	@Override
	public void setGroupClickStatus(int groupPosition, int status) {
		// TODO Auto-generated method stub
		groupStatusMap.put(groupPosition, status);
	}

	@Override
	public int getGroupClickStatus(int groupPosition) {
		// TODO Auto-generated method stub
		if (groupStatusMap.containsKey(groupPosition)) {
			return groupStatusMap.get(groupPosition);
		} else {
			return 0;
		}
	}

}
